package com.pudu.mydemo.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by fanyp on 2016/8/30.
 */
public class RobotFileUtils {

    public static void writePathFile(String pathName, String fileName, String data)
    {
        try {
            String [] paths = pathName.split(File.separator);
            StringBuffer fullPath=new StringBuffer();

            for (int i = 0; i < paths.length; i++) {
                fullPath.append(paths[i]).append(File.separator);
                File file=new File(fullPath.toString());
                if(!file.exists()){
                    file.mkdir();
                }
            }

            String strMyPathFile = pathName + File.separator + fileName;
            File file= new File(strMyPathFile);
            FileWriter fw;

            fw = new FileWriter(file);
            fw.write(data);
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void writeAppendPathFile(String pathName, String fileName, String data)
    {
        try {
            String [] paths = pathName.split(File.separator);
            StringBuffer fullPath=new StringBuffer();

            for (int i = 0; i < paths.length; i++) {
                fullPath.append(paths[i]).append(File.separator);
                File file=new File(fullPath.toString());
                if(!file.exists()){
                    file.mkdir();
                }
            }

            String strMyPathFile = pathName + File.separator + fileName;
            File file= new File(strMyPathFile);

            if(file.exists() == false)
            {
                file.createNewFile();
            }

            FileWriter filerWriter = new FileWriter(file, true);
            BufferedWriter bufWriter = new BufferedWriter(filerWriter);

            bufWriter.write(data);
            bufWriter.newLine();
            bufWriter.close();
            filerWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String readPathFile(String pathName, String fileName)
    {
        String content = null;
        try {
            String strMyPathFile = pathName + File.separator + fileName;
            File file = new File(strMyPathFile);
            if(file.exists() == false)
            {
                return null;
            }

            FileReader reader = new FileReader(file);
            char[] chars = new char[(int) file.length()];
            reader.read(chars);
            content = new String(chars);
            reader.close();

        }catch(Exception e) {
            e.printStackTrace();
            return null;
        }

        return content;
    }

    public static void buildDir(String pathName)
    {
        try {
            String [] paths = pathName.split(File.separator);
            StringBuffer fullPath=new StringBuffer();

            for (int i = 0; i < paths.length; i++) {
                fullPath.append(paths[i]).append(File.separator);
                File file=new File(fullPath.toString());
                if(!file.exists()){
                    file.mkdir();
                }
            }
        }catch(Exception e) {
            e.printStackTrace();
        }

    }

    public static String movePathFile(String srcFileName, String destDirName)
    {
        File srcFile = new File(srcFileName);
        if(!srcFile.exists() || !srcFile.isFile())
            return "";

        File destDir = new File(destDirName);
        if (!destDir.exists())
            destDir.mkdirs();

        srcFile.renameTo(new File(destDirName + File.separator + srcFile.getName()));

        return (destDirName + File.separator + srcFile.getName());
    }


}
